/*
	Date: 2017-4-25
    Author:H.Y
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 4096
#define KEYBUFFER_SIZE 512
#define MAX_KEY_SIZE 8

int ConvertByteToBinary(unsigned char *keybuffer, char *strbuffer, int keyslength, int keysize)
{
    /*
        参数：keybuffer(读取的数据),strbuffer(存放转换结果的内存), keyslength(读取数据量),keysize(单个区域的bit数量)
    */
    unsigned char flag = 0x01;
    int i, j;
    for (i = 0; i < keyslength && i < KEYBUFFER_SIZE; i++)
    {
	flag = 0x01;
	for (j = 0; j < keysize && j < MAX_KEY_SIZE; j++)
	{
	    if (flag & keybuffer[i])
		strbuffer[keysize * i + keysize - 1 - j] = '1';
	    else
		strbuffer[keysize * i + keysize - 1 - j] = '0';
	    flag = flag << 1;
	}
    }
    return 0;
}

int FileBinToChar(char *filename, char *outfilename, unsigned int maxSize)
{
    /*
        参数：filename(输入文件路径),outfilename(输出文件路径),maxSize(转换最大byte)
    */
    unsigned int sizeTemp = 0;
    int keysize = 8;
    int keyslength = BUFFER_SIZE / keysize;
    int readlength = 0;
    char *strbuffer = (char *)malloc(4096 * sizeof(char));
    unsigned char *keybuffer = (unsigned char *)malloc(keyslength * sizeof(char));
    FILE *file = NULL;
    FILE *outfile = NULL;

    file = fopen(filename, "rb");
    outfile = fopen(outfilename, "wb");
    if (file == NULL)
    {
	printf("input file -%s- open error", filename);
	return 0;
    }
    if (outfile == NULL)
    {
	printf("output file --%s-- open error", outfilename);
	return 0;
    }
    while ((readlength = fread(keybuffer, keysize / 8, KEYBUFFER_SIZE, file)) > 0)
    {
	ConvertByteToBinary(keybuffer, strbuffer, readlength, keysize);
	fwrite(strbuffer, 1, readlength * keysize, outfile);
	sizeTemp += (unsigned int)readlength;
	if (maxSize > 0 && sizeTemp >= maxSize)
	    break;
    }
    fclose(outfile);

    return 0;
}

int CreateZeroFile(char *outfilename, int filesize)
{
    /*
        参数:filesize(文件的byte大小),outfilename(输出文件路径)
    */
    int loopRound = 0;
    int i = 0;
    int leftByte = 0;
    FILE *outfile = NULL;
    loopRound = filesize / KEYBUFFER_SIZE;
    leftByte = filesize % KEYBUFFER_SIZE;
    outfile = fopen(outfilename, "wb");
    if (outfile == NULL)
    {
	printf("output file open error");
	return 0;
    }

    char *outStreamBuffer = (char *)malloc(4096 * sizeof(char));
    for (i = 0; i < loopRound; i++)
    {
	fwrite(outStreamBuffer, sizeof(char), 4096, outfile);
    }
    if (leftByte)
    {
	fwrite(outStreamBuffer, sizeof(char), leftByte, outfile);
    }
    fclose(outfile);
    return 0;
}

int main(int argc, char **argv)
{
    /*
        argv：-Z（生成全0的数据）outfilename size(byte), -C（进行转换）infilename outfilename
    */
    int i = 0;
    int filesize = 0;
    for (i = 0; i < argc; i++)
    {
		if (strcmp(argv[i], "size") == 0)
		{
			if (argc >= i + 1)
				filesize = atoi(argv[i + 1]);
			else
			{
				printf("param error: no size");
				return 0;
			}
		}
    }
	printf("file size is %d\n",filesize);

    if (argc > 1)
    {
		if (strcmp(argv[1], "-C") == 0 && argc >= 4)
		{
			char *filename = argv[2];
			char *outfilename = argv[3];
			strcpy(outfilename, filename);
			strcat(outfilename, ".char");
			FileBinToChar(filename, outfilename, filesize);
		}
		else if (strcmp(argv[1], "-Z") == 0 && argc >= 4)
		{
			printf("create zero file to %s\n", argv[2]);
			CreateZeroFile(argv[2], filesize);
		}
    }
    else
    {
		printf("params errors %d \n", argc);
    }
    return 0;
}